package com.example.springboot.config;

import io.seata.rm.datasource.DataSourceProxy;
import io.seata.rm.datasource.xa.DataSourceProxyXA;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

/**
 * 配置数据库代理
 * <p>
 * <p>
 *
 * @author Zhang.Z
 * @version 2025-09-23
 */
@Configuration
public class DataSourceConfig {

    /**
     * 原始数据源
     */
    @Bean
//    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource hikariDataSource(DataSourceProperties dataSourceProperties) {
        return dataSourceProperties.initializeDataSourceBuilder()
                .type(com.zaxxer.hikari.HikariDataSource.class)
                .build();
    }

    /**
     * seata代理数据源
     */
    @Bean
    @Primary
    public DataSource dataSource(DataSource hikariDataSource) {
        return new DataSourceProxyXA(hikariDataSource);
    }
}
